package com.test.stuff;

import java.util.Calendar;

public class Transaction{

	protected int id;
	protected long date; 
	protected Account account;
	protected Group group;
	protected double amount;
	
	protected static Logger logger = new Logger(Transaction.class.getSimpleName());
	
	//////
	// constructors

	public Transaction(int id, double amount, Account account, Group group) throws Exception{
		this(id, amount, account, group, getCurrentTime());
	}
	
	public Transaction(int id, double amount, Account account, Group group, long date) throws Exception{
		if(id<1)
			throw new Exception("Cannot init Transaction because id is invalid");
		if(date<0)
			throw new Exception("Cannot init Transaction because date is invalid");
		if(account==null)
			throw new Exception("Cannot init Transaction because account is invalid");
		if(group==null)
			throw new Exception("Cannot init Transaction because group is invalid");
		
		this.id = id;
		this.date = date;
		this.account = account;
		this.group = group;
		this.amount = amount;
	}
	
	//////
	// overrides
	
	@Override
	public int hashCode(){
		int prime = 191;
		return (int)(Math.pow((Math.pow((id*prime), account.toString().length())+date)*amount, group.toString().hashCode()));
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this==obj)
			return true;
		
		if(obj==null){
//	    	logger.log("obj null");
			return false;
		}
		
		if (this.getClass() != obj.getClass()) {
//	    	logger.log("classes() don't match");
//	    	logger.log(this.getClass()+" vs "+obj.getClass());
	        return false;
	    }
		
	    final Transaction other = (Transaction) obj;

	    // verify the account for the transactions
	    if (!this.account.equals(other.account)){
//	    	logger.log("account don't match");
//	    	logger.log(this.account+" vs "+other.account);
	    	return false;
	    }
	    
	    // verify the event for the transactions
	    if (!this.group.equals(other.group)){
//	    	logger.log("event don't match");
//	    	logger.log(this.event+" vs "+other.event);
	    	return false;
	    }
	    
	    //verify the amount for the transactions
	    if (this.amount != other.amount){
//	    	logger.log("amount don't match");
//	    	logger.log(this.amount+" vs "+other.amount);
	    	return false;
	    }
	    
	    // verify the date for the transactions
	    if (this.date != other.date){
//	    	logger.log("date don't match");
//	    	logger.log(this.date+" vs "+other.date);
	    	return false;
	    }
	    
	    // finally verify id is the same value
	    if (this.id != other.id) {
//	    	logger.log("id don't match");
//	    	logger.log(this.id+" vs "+other.id);
	        return false;
	    }
	    
	    // if name and id are the same, then they are equal
	    return true;
	}
	
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();

		result.append("Transaction Details -- ");
		result.append("ID: "+this.id+", ");
		result.append("Date: "+this.date+", ");
		result.append("Account: "+this.account+", ");
		result.append("Group: "+this.group+", ");
		result.append("Amount: "+this.amount);
		
		return result.toString();
	}
	
	//////
	// setters

	public void setId(int id){
		if(id<1)
			return;
		this.id = id;
	}
	
	public void setDate(long date){
		if(date<0)
			return;
		this.date = date;
	}
	
	public void setAccount(Account account){
		if(account==null)
			return;
		this.account = account;
	}
	
	public void setGroup(Group group){
		if(group==null)
			return;
		this.group = group;
	}
	
	public void setAmount(double amount){
		this.amount = amount;
	}
	
	//////
	// getters
	
	public int getId(){ return this.id; }
	public long getDate(){ return this.date; }
	public Account getAccount(){ return this.account; }
	public Group getGroup(){ return this.group; }
	public double getAmount(){ return this.amount; }
	
	//////
	// methods
	
	public static long getCurrentTime(){
		return Calendar.getInstance().getTimeInMillis();
	}
}